﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace StockLib.Indicator
{
    public partial class StockKIndicator : StockIndicator
    {
        public override void DrawGraphTitle(Graphics g, Rectangle rect)
        {
            int y = (rect.Top + rect.Bottom) / 2;
            int x = rect.Left;
            string textTitle = string.Format("{0} ({1})", stock.Name, "日线");
            TextRenderer.DrawText(g, textTitle, font, new Point(x, y), StockProfile.Colors.GraphTextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
            x += TextRenderer.MeasureText(textTitle, font).Width;

            if (showIndicator && otherIndicator != null)
            {
                rect.X = x;
                rect.Width = rectGraph.Right - x;
                otherIndicator.DrawGraphTitle(g, rect);
            }
        }

        public override void DrawGraphData(Graphics g)
        {
            DrawKData(g);

            if (showIndicator && otherIndicator != null)
            {
                otherIndicator.DrawGraphData(g);
            }

            DrawMarker(g);
        }

        private void DrawKData(Graphics g)
        {
            for (int index = indexStart; index <= indexEnd; index++)
            {
                KData data = kData[index];
                int xPos = GetXPos(index);
                int yHigh = GetYPos(data.High);
                int yLow = GetYPos(data.Low);

                Pen pen = StockProfile.Pens.StLinePen;
                Brush brush = StockProfile.Brushes.StLineBrush;
                if (data.Close > data.Open)
                {
                    if (klineType == KLineType.HollowLine)
                    {
                        pen = StockProfile.Pens.UpLinePen;
                        brush = StockProfile.Brushes.BackBrush;
                    }
                    else if (klineType == KLineType.SolidLine)
                    {
                        pen = StockProfile.Pens.UpLinePen;
                        brush = StockProfile.Brushes.UpLineBrush;
                    }
                }
                if (data.Close < data.Open)
                {
                    pen = StockProfile.Pens.DnLinePen;
                    brush = StockProfile.Brushes.DnLineBrush;
                }

                // 绘制影线                
                g.DrawLine(pen, xPos, yLow, xPos, yHigh);

                // 绘制实体线
                if (thickness >= 1)
                {
                    Rectangle kRect = new Rectangle();
                    kRect.X = xPos - thickness;
                    kRect.Y = GetYPos(Math.Max(data.Close, data.Open));
                    kRect.Width = thickness * 2;
                    kRect.Height = GetYHeight(data.Close - data.Open);

                    if (kRect.Height > 0)
                    {
                        g.FillRectangle(brush, kRect);
                        g.DrawRectangle(pen, kRect);
                    }
                }
            }
        }

        private void DrawMarker(Graphics g)
        {
            const int xPadding = 2;
            const int yPadding = 2;
            const int arrowWidth = 10;
            const int arrowOffset = 3;

            for (int index = indexStart; index <= indexEnd; index++)
            {
                KData data = kData[index];

                // 绘制最小值最大值
                if (index == indexMin || index == indexMax)
                {
                    int xPos = GetXPos(index);
                    int yPos = (index == indexMin) ? (GetYPos(data.Low) + yPadding) : (GetYPos(data.High) - yPadding);

                    string textMinMax = string.Format("{0:#0.00}", (index == indexMin) ? valueMin : valueMax);
                    int textWidth = TextRenderer.MeasureText(textMinMax, font).Width;
                    int textSign = (xPos + xPadding + arrowWidth + textWidth > rectGraph.Right) ? -1 : 1;

                    Point point = new Point(xPos + xPadding * textSign, yPos);
                    using (Pen pen = new Pen(StockProfile.Colors.GraphTextColor))
                    {
                        g.DrawLine(pen, point.X, point.Y, point.X + arrowWidth * textSign, point.Y);
                        g.DrawLine(pen, point.X, point.Y, point.X + arrowOffset * textSign, point.Y - 3);
                        g.DrawLine(pen, point.X, point.Y, point.X + arrowOffset * textSign, point.Y + 3);
                    }
                    point.X += arrowWidth * textSign;

                    if (textSign == -1) point.X -= textWidth;
                    TextRenderer.DrawText(g, textMinMax, font, point, StockProfile.Colors.GraphTextColor, TextFormatFlags.VerticalCenter);
                }
            }
        }

        protected override void PrepareDrawSetting(Rectangle rectAll)
        {
            base.PrepareDrawSetting(rectAll);

            graphMargin.Top = 20;
            graphMargin.Bottom = 20;

            if (otherIndicator != null)
            {
                otherIndicator.DuplicateDrawSetting(this);
            }
        }
    }
}
